<?php
class resource_advertise_device_concept extends system_concept_abstract {
	
	public function init() {
		$this->setModel('resource_advertise_device_model', 'device');
		$this->setModelCols('device', array('item_id', 'device_key'));
	}

	public function save($row) {
		$row->set('item_id', intval($row->get('item_id')));
		if(!$row->getItemId()) {
			throw new resource_exception("Item ID can not be empty.");
		}
		if(!$row->getDeviceKey()) {
			throw new resource_exception("Device Key can not empty.");
		}
		if(!$this->fetch(array('item_id = ?' => $row->getItemId(), 'device_key = ?' => $row->getDeviceKey()))) {
			$this->getModel('device')->insert($row->getModelData('device'));
		} else {
			$row->update();
		}
		return $row;
	}
}